/*   
 * Copyright (c) 2002, Intel Corporation. All rights reserved.
 * Created by:  rolla.n.selbak REMOVE-THIS AT intel DOT com
 * This file is licensed under the GPL license.  For the full content
 * of this license, see the COPYING file at the top level of this 
 * source tree.
 *
 *  void pthread_cleanup_pop(int execution); 
 *
 * Shall remove the routine at the top of the calling thread's cancelation cleanup stack and
 * optionally invoke it (if execute is non-zero).
 *  
 * STEPS:
 * 1. Create a thread
 * 2. The thread will push 3 cleanup handler routines, then it will call the pop function 3
 *    times.
 * 3. Verify that the cleanup handlers are popped in order.
 * 
 */

#include <pthread.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include "posixtest.h"

# define CLEANUP_NOTCALLED 0 
# define CLEANUP_CALLED 1

int pthread_cleanup_pop_1_3_cleanup_flag[3];		/* Array to hold the cleanup flags for the 3 cleanup handlers */
int pthread_cleanup_pop_1_3_i;

/* 3 Cleanup handlers */
void pthread_cleanup_pop_1_3_a_cleanup_func1(void *flag_val)	
{
	pthread_cleanup_pop_1_3_cleanup_flag[pthread_cleanup_pop_1_3_i] = 1;
	pthread_cleanup_pop_1_3_i++;
	return;
}

void pthread_cleanup_pop_1_3_a_cleanup_func2(void *flag_val)	
{
	pthread_cleanup_pop_1_3_cleanup_flag[pthread_cleanup_pop_1_3_i] = 2;
	pthread_cleanup_pop_1_3_i++;
	return;
}

void pthread_cleanup_pop_1_3_a_cleanup_func3(void *flag_val)	
{
	pthread_cleanup_pop_1_3_cleanup_flag[pthread_cleanup_pop_1_3_i] = 3;
	pthread_cleanup_pop_1_3_i++;
	return;
}

/* Function that the thread executes upon its creation */
void *pthread_cleanup_pop_1_3_a_thread_func()
{
	pthread_cleanup_push(pthread_cleanup_pop_1_3_a_cleanup_func1, NULL);
	pthread_cleanup_push(pthread_cleanup_pop_1_3_a_cleanup_func2, NULL);
	pthread_cleanup_push(pthread_cleanup_pop_1_3_a_cleanup_func3, NULL);
	pthread_cleanup_pop(1);
	pthread_cleanup_pop(1);
	pthread_cleanup_pop(1);

	pthread_exit(0);
	return NULL;
}

int pthread_cleanup_pop_1_3()
{
	pthread_t new_th;

	/* Initializing values */
	for(pthread_cleanup_pop_1_3_i = 0;pthread_cleanup_pop_1_3_i < 3;pthread_cleanup_pop_1_3_i++)
		pthread_cleanup_pop_1_3_cleanup_flag[pthread_cleanup_pop_1_3_i] = 0;
	pthread_cleanup_pop_1_3_i = 0;
	
	/* Create a new thread. */
	if(pthread_create(&new_th, NULL, pthread_cleanup_pop_1_3_a_thread_func, NULL) != 0)
	{	
		perror("Error creating thread\n");
		return PTS_UNRESOLVED;
	}

	/* Wait for thread to end execution */
	if(pthread_join(new_th, NULL) != 0)
	{
		perror("Error in pthread_join()\n");
		return PTS_UNRESOLVED;
	}
	
	/* Verify that the cancellation handlers are popped in order, that is:
	 * 3, 2, then 1. */
	if((pthread_cleanup_pop_1_3_cleanup_flag[0] != 3) || (pthread_cleanup_pop_1_3_cleanup_flag[1] != 2) || (pthread_cleanup_pop_1_3_cleanup_flag[2] != 1))
	{
		printf("Test FAILED: Cleanup handlers not popped in order, expected 3,2,1, but got:\n");
		printf("%d, %d, %d\n", pthread_cleanup_pop_1_3_cleanup_flag[0], pthread_cleanup_pop_1_3_cleanup_flag[1], pthread_cleanup_pop_1_3_cleanup_flag[2]);
		return PTS_FAIL;
	} 
		
	printf("Test PASSED\n");
	return PTS_PASS;	
}


